<?php

namespace AuctionAppApi\Controller;
use AuctionCore\System\AuctionException;
/**
 * 抽奖
 */
class RaffleController extends BaseController{
    //抽奖立减
    public function raffle() {
        $order_id = I('request.order_id');
        $type = I('request.type',1);
        if ($type<1) {
            AuctionException::make_throw('1600002'); 
        }
        if ($order_id<1) {
            AuctionException::make_throw('700002'); 
        }
        //判断状态
        $order = new \AuctionCore\AuctionOrder\AuOrder($order_id);
        $order_info = $order->detail();
        if ($type == 1) {
            //!in_array($order_info['dealer_pay_status'], array(1,2))
            if($order_info['dealer_pay_status'] != 0 || $order_info['status'] != 8){
                $this->assign('msg',AuctionException::$codes['1600001']);
                $this->display('Public/error');
                exit;
            }
        }elseif ($type == 2) {

            if(!in_array($order_info['status'], array(11,13))){
                $this->assign('msg',AuctionException::$codes['1600001']);
                $this->display('Public/error');
                exit;
            }
        }
        $Raffle = new \AuctionCore\Other\Raffle();
        // $map = array('order_id'=>$order_id,'state'=>0);
        $card_info = $Raffle->find_card_activity($order_id,$type);

        if (!$card_info) {
            $this->display(AuctionException::$codes['1600000']);
            $this->display('Public/error');
            exit;
        }else{
            $starttime = date('Y-m-d',strtotime($card_info['starttime']));
            $endtime = date('Y-m-d',strtotime($card_info['endtime']));
            $start = explode('-',$starttime);
            $starttime = $start[0]."年".$start[1]."月".$start[2]."日";
            $end = explode('-',$endtime);
            $endtime = $end[0]."年".$end[1]."月".$end[2]."日";
            $this->assign('starttime', $starttime);
            $this->assign('endtime', $endtime);
        }
        $this->assign('type', $type);     
        $this->assign('order_id', $order_id);
        if ($type == 1) {
            $this->display('./Raffle/raffle');
        }else{
            $this->display('Raffle/raffleCoupon');
        }
    }

    //根据订单找到对应卡券面值
    public function get_face_value(){
        $order_id = I('request.order_id');
        $type = I('request.type',1);
        if ($type<1) {
            AuctionException::make_throw('1600002'); 
        }
        if ($order_id<1) {
            AuctionException::make_throw('700002'); 
        }
        //判断状态
        $order = new \AuctionCore\AuctionOrder\AuOrder($order_id);
        $order_info = $order->detail();
        // if(!in_array($order_info['dealer_pay_status'],array(1,2))){
        //     AuctionException::make_throw('1600001');
        // }

        $Raffle = new \AuctionCore\Other\Raffle();
        // $map = array('order_id'=>$order_id);
        // $card_info = $Raffle->find_cards($map);
        $card_info = $Raffle->find_card_activity($order_id,$type);

        $state = $card_info['state'];
        if ($state == 1) {
            echo \AuctionCore\Common\Util::auto_jsonp_or_json($_REQUEST,array('code'=>201,'msg'=>'您已经抽过奖了'));
            exit;
        }
        $face_value = $Raffle->return_face_value($order_id,$type);
        if ($face_value) {
            $this->api_200($face_value);    
        }else{
            AuctionException::make_throw('100065'); 
        }
    }


    //活动页面
    public function raffleActivity(){
        $activity_no = I('request.activity_no');
        $activityInfo = M('activity')->where("activity_no=".$activity_no)->find();
        $starttime = date('Y-m-d',strtotime($activityInfo['starttime']));
        $endtime = date('Y-m-d',strtotime($activityInfo['endtime']));
        $start = explode('-',$starttime);
        $starttime = $start[0]."年".$start[1]."月".$start[2]."日";
        $end = explode('-',$endtime);
        $endtime = $end[0]."年".$end[1]."月".$end[2]."日";
        $this->assign('starttime', $starttime);
        $this->assign('endtime', $endtime);
        $this->assign('activity_no', $activity_no);
        $tpl = "Raffle/activity/activity".$activity_no;
        $this->display($tpl);
    }
    //活动页面  找到最近活动车商电话以及卡券面值
    public function get_dealer_phone_card(){
        $activity_no = I('request.activity_no');
        $Raffle = new \AuctionCore\Other\Raffle();
        $top_dealer_info = $Raffle->get_dealer_phone_card($activity_no);
        if ($top_dealer_info) {
            echo \AuctionCore\Common\Util::auto_jsonp_or_json($_REQUEST,array('code'=>200,'msg'=>'','data'=>json_encode($top_dealer_info)));
            exit;
        }else{
            echo \AuctionCore\Common\Util::auto_jsonp_or_json($_REQUEST,array('code'=>210,'msg'=>''));
            exit;
        }

    }

    //公用活动页面
    public function common_activity(){
        $activity_no = I('request.activity_no');
        $first_activity_no = explode(',', $activity_no)[0];
        $activityInfo = M('activity')->where("activity_no=".$first_activity_no)->find();
        $starttime = date('Y-m-d',strtotime($activityInfo['starttime']));
        $endtime = date('Y-m-d',strtotime($activityInfo['endtime']));
        $start = explode('-',$starttime);
        $starttime = $start[0]."年".$start[1]."月".$start[2]."日";
        $end = explode('-',$endtime);
        $endtime = $end[0]."年".$end[1]."月".$end[2]."日";
        $this->assign('starttime', $starttime);
        $this->assign('endtime', $endtime);
        $this->assign('activity_no', $activity_no);
        $this->display();
    }
}